home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Commun⁄Network / Telnet 2.5.src.ThinkC / source / slip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-21  |  3.5 KB  |  182 lines  |  [TEXT/KAHL]

  1. /*
  2. *   SLIP routines
  3. *
  4. *   new with NCSA/BYU Telnet version 2.4.15
  5. *
  6. *   by Jim Logan Feb 1992
  7. *
  8. */
  9.  
  10. #include <string.h>
  11.  
  12. #include "protocol.h"
  13. #include "ip.h"
  14. #include "data.h"
  15. #include "wind.h"
  16.  
  17. #define input_size 8192
  18. #define input_buf_size 12288
  19. #define output_buf_size 8192
  20. #define SLIP_IN_BUFS 64
  21.  
  22. #define SLIP_END 0xc0
  23. #define SLIP_ESC 0xdb
  24. #define SLIP_ESC_END 0334
  25. #define SLIP_ESC_ESC 0335
  26.  
  27. static char slip_end = 0xc0;
  28. static char slip_esc_end[] = { 0xdb,0xdc };
  29. static char slip_esc_esc[] = { 0xdb,0xdd };
  30.  
  31. short
  32.     old_received = 0,
  33.     received = 0,
  34.     slipfirstbuf = 0,
  35.     sliplastbuf = 0;
  36.  
  37. extern WindRec *screens;        /* From "maclook.c" */
  38.  
  39. extern short slip_connection;
  40.  
  41. extern unsigned char SLIP_ip_number[];
  42.  
  43. unsigned char *slipinptr[SLIP_IN_BUFS],*slipinbuf,*slipoutbuf;
  44.  
  45.  
  46. initslip() {
  47. long mysize;
  48.     mysize = input_buf_size;
  49.     slipinbuf = (unsigned char *) NewPtr(mysize);
  50.     mysize = output_buf_size;
  51.     slipoutbuf = (unsigned char *) NewPtr(mysize);
  52.     slipinptr[0] = slipinbuf;
  53. }
  54.  
  55.  
  56. switchtoslip(pnum) int pnum; {
  57.     struct port *p;
  58.     int slipscrn;
  59.  
  60.     if (pnum < 0 || pnum >= NPORTS)
  61.         return;
  62.  
  63.     if (NULL == (p = portlist[pnum]))
  64.         return;
  65.  
  66.     p->state = SCLOSED;
  67.     p->in.port = p->out.port = 0;
  68.  
  69.     slip_connection = 1;
  70.  
  71. /* Close the serial connection, and require the operator 
  72.    to open SLIP sessions with the "Open" function 
  73.    under the "File" menu. */
  74.  
  75.     slipscrn = WindByPort(pnum);
  76.     if (slipscrn >= 0) 
  77.       destroyport(slipscrn);
  78.  
  79.     setupport(0);                /* Open a connection, maybe even SLIP */                
  80. }
  81.  
  82.  
  83. int SLIPdlayersend(unsigned char *ptr,int size) {
  84.     int pos;
  85.  
  86.     ptr += sizeof(DLAYER);
  87.     size -= sizeof(DLAYER);
  88.     pos = 1;
  89.     slipoutbuf[0] = slip_end;
  90.     while (size > 0) {
  91.         switch (*ptr) {
  92.             case SLIP_END:
  93.                 slipoutbuf[pos++] = slip_esc_end[0];
  94.                 slipoutbuf[pos++] = slip_esc_end[1];
  95.                 break;
  96.             case SLIP_ESC:
  97.                 slipoutbuf[pos++] = slip_esc_esc[0];
  98.                 slipoutbuf[pos++] = slip_esc_esc[1];
  99.                 break;
  100.             default:
  101.                 slipoutbuf[pos++] = *ptr;
  102.         }
  103.         ptr++;
  104.         size--;
  105.     }
  106.     slipoutbuf[pos++] = slip_end;
  107.     write_serial(slipoutbuf,pos);
  108.     return(size);
  109. }
  110.  
  111.  
  112. SLIPreceive(unsigned char *ptr,int size) {
  113.     while (size > 0) {
  114.         switch (*ptr) {
  115.             case SLIP_END:
  116.  
  117.                 if (received != old_received) {
  118.  
  119. /* Don't allow the IP header to begin on         /* BYU 2.4.19 */
  120. /* an odd byte or MacPlus will crash.            /* BYU 2.4.19 */
  121.                     if (received > input_size)
  122.                         received = 0;
  123.                     else if (received & 1)        /* BYU 2.4.19 */
  124.                         received++;                /* BYU 2.4.19 */
  125.  
  126.                     sliplastbuf++;
  127.                     if (sliplastbuf >= SLIP_IN_BUFS)
  128.                         sliplastbuf = 0;
  129.  
  130.                     /* Prepare the next buffer space */
  131.                     slipinptr[sliplastbuf] = slipinbuf + received;
  132.  
  133.                     if ((slipinbuf + received) == slipinptr[slipfirstbuf])
  134.                         SysBeep(4);                            /* Buffer overflow/overrun */
  135.  
  136.                     old_received = received;
  137.                 }
  138.                 break;
  139.             case SLIP_ESC:
  140.                 size--;
  141.                 if (size <= 0)
  142.                     return;
  143.                 ptr++;
  144.                 switch (*ptr) {
  145.                     case SLIP_ESC_END:
  146.                         if (received < input_size)
  147.                             slipinbuf[received++] = SLIP_END;
  148.                         break;
  149.                     case SLIP_ESC_ESC:
  150.                         if (received < input_size)
  151.                             slipinbuf[received++] = SLIP_ESC;
  152.                         break;
  153.                     default:
  154.                         slipinbuf[received++] = *ptr;        /* Hmmmm, questionable! */
  155.                         break;
  156.                 }
  157.                 break;
  158.             default:
  159.                 if (received < input_buf_size)
  160.                     slipinbuf[received++] = *ptr;
  161.         }
  162.         size--;
  163.         ptr++;
  164.     }
  165. }
  166.  
  167.  
  168. int SLIPdemux(int all) {
  169.     int nmuxed;
  170.  
  171.     nmuxed = 0;
  172.     while (slipfirstbuf != sliplastbuf) {
  173.         ipinterpret((IPKT *)(slipinptr[slipfirstbuf] - sizeof(DLAYER)),FROM_SLIP);
  174.         slipfirstbuf++;
  175.         if (slipfirstbuf >= SLIP_IN_BUFS)
  176.             slipfirstbuf = 0;
  177.         nmuxed++;
  178.     }
  179.  
  180.     return(nmuxed);
  181. }
  182.